package com.sxsihe.heartmate;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.BufferedReader;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;


/**
 * Created by 志华 on 2016/2/18.
 */
public class EcgReader implements Runnable{


    Handler mHandler;
    Thread mThread;
    boolean mRunning = false;
    int mHeartRatio = 0;
    ArrayList<Integer> mRaws = new ArrayList<Integer>();
    EcgReader() {
        mThread = new Thread(this);
        mThread.start();
    }

    double stamp = 0;
    /*设置读取的文件*/
    void setEcgFile(String fileName)
    {
        try
        {
            FileInputStream instream = new FileInputStream(fileName);
            if (instream != null)
            {
                InputStreamReader inputreader = new InputStreamReader(instream);
                BufferedReader buffreader = new BufferedReader(inputreader);
                String line;
                while ((line = buffreader.readLine()) != null) {
                    String[] span = line.split("\\|");
                    if (span[0].equals("aver_heart_rate")) {
                        mHeartRatio = Integer.parseInt(span[1]);
                    } else {
                        int raw = Integer.parseInt(span[1]);
//                        if(Double.parseDouble(span[0])==stamp)
//                            continue;
//                        stamp = Double.parseDouble(span[0]);
                        mRaws.add(raw);
                    }
                }
                instream.close();
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    void registerHandle(Handler h) {
        mHandler = h;
    }

    /*开始读取数据*/
    void start()
    {
        mRunning = true;
    }

    /*停止读取数据*/
    void stop()
    {
        mRunning = false;
    }

    @Override
    public void run()
    {
        while(true)
        {
            if(mRunning) {
                try {
                    Message message = new Message();
                    message.arg1 = mHeartRatio;
                    message.what = 1;
                    mHandler.sendMessage(message);

                    for (int i = 0; i < mRaws.size(); i++)
                    {
                        if(!mRunning)
                        {
                            wait();
                        }
                        int raw = mRaws.get(i);
                        message = new Message();
                        message.arg1 = raw;
                        message.what = 0;
                        mHandler.sendMessage(message);
                        Thread.sleep(3);
                    }
                    stop();
                    message = new Message();
                    message.what = 2;
                    mHandler.sendMessage(message);

                } catch (Exception e) {}
            }
        }

    }
}
